//
//  JKProgressView.swift
//  JKProgressView
//
//  Created by albert on 16/6/24.
//  Copyright © 2016年 albert. All rights reserved.
//

import UIKit

class JKProgressView: UIView {
    
    /** 进度 */
    var progress: CGFloat = 0 {
        didSet{
            setNeedsDisplay()
        }
    }

    override func drawRect(rect: CGRect) {
        
        if progress >= 1 {
            return
        }
        
        // 1.准备数据
        let X = rect.width * 0.5
        let Y = (rect.height > JKScreenH) ? (JKScreenH * 0.5) : (rect.height * 0.5)
        
        let center = CGPoint(x: X, y: Y)
        
        let radius: CGFloat = 30//min(width, height)
        let startEngle = -CGFloat(M_PI_2)
        let endEngle = CGFloat(M_PI * 2) * progress  + startEngle
        
        // 2.最外面白色圆环
        let path = UIBezierPath(arcCenter: center, radius: radius - 2, startAngle: startEngle, endAngle: CGFloat(M_PI * 2), clockwise: true)
        UIColor.whiteColor().set()
        path.stroke()
        
        // 2.灰色透明背景
        let path1 = UIBezierPath(arcCenter: center, radius: radius - 2, startAngle: startEngle, endAngle: CGFloat(M_PI * 2), clockwise: true)
        path1.addLineToPoint(center)
        path1.closePath()
        UIColor.grayColor().colorWithAlphaComponent(0.5).setFill()
        path1.fill()
        
        // 3.进度
        let path2 = UIBezierPath(arcCenter: center, radius: radius - 5, startAngle: startEngle, endAngle: endEngle, clockwise: true)
        path2.addLineToPoint(center)
        path2.closePath()
        UIColor.whiteColor().setFill()
        path2.fill()
    }
}
